package ptij.vocoder.baseModel;

import java.io.*;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.StringToken;
import ptolemy.data.type.*;
import ptolemy.data.expr.Parameter;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// FrameGen
/** 

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class FrameGen extends TypedAtomicActor {

    public FrameGen(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        output = new TypedIOPort(this, "output", false, true);
        output.setTypeEquals(BaseType.OBJECT);
        
        fileName = new Parameter(this, "fileName", 
             new StringToken("spch_unx.inp"));
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports

    // Output Ports
    public TypedIOPort output;

    public Parameter fileName;
    
    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
        
        StringToken st = (StringToken) fileName.getToken();
        String fname = (String) st.stringValue();
        
        try {
            File f = new File(fname);
            FileInputStream fi = new FileInputStream(f);
            _dataIn = new DataInputStream(fi);
        }
        catch(IOException ex) {
            System.out.println(ex);
            _dataIn = null;
        }
    }

    public boolean prefire() throws IllegalActionException {
        _nextFrame = new Short[Cnst.L_FRAME];
        // Yes, it sucks.....
        for(int i = 0;i < Cnst.L_FRAME;i++) {
            try {
                _nextFrame[i] = new Short((short)(_dataIn.readShort() & (short)0xFFF8));
            }
            catch(IOException ioex) {
                return false;
            }
        }
        return true;
    }
    
    public void fire() throws IllegalActionException {
        ObjectToken obj = new ObjectToken(_nextFrame);
        output.broadcast(obj);
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    private Short [] _getNextFrame() {
        Short [] frame = new Short[Cnst.L_FRAME];
        // Yes, it sucks.....
        for(int i = 0;i < Cnst.L_FRAME;i++) {
            try {
                frame[i] = new Short(_dataIn.readShort());
            }
            catch(IOException ioex) {
                
                System.out.println(ioex);
                frame[i] = new Short((short)0);
            }
        }
        return frame;
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private DataInputStream _dataIn;
    private Short [] _nextFrame;
}
